<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@taglib uri="/authUtil"  prefix="authUtil"%>
	<button id="addButton">增加新管理员</button>
	<c:if test="${empty requestScope.adminList }">
			<h1>没有人员信息</h1>
	</c:if>
	<c:if test="${!empty requestScope.adminList }">
		<table>
			<tr>
				<th>用户名</th>
				<th>姓名</th>
				<th>用户身份</th>
				<th>用户数据管理</th>
				<th>房间类型管理</th>
				<th>房间数据管理</th>
				<th>入住信息管理</th>
				<th>换房信息管理</th>
				<th>续住信息管理</th>
				<th>财务信息管理</th>
				<th>注册日期</th>
				<th>处理</th>
			</tr>
			<c:forEach items="${requestScope.adminList }" var="admin">
				<tr>
					<td>${ admin.username}</td>
					<td>${admin.name }</td>
					<td>${admin.roleCode }</td>
					<td>
						<c:if test="${authUtil:isAuth(admin.auth,0) }">
							√
						</c:if>
					</td>
					<td>
						<c:if test="${authUtil:isAuth(admin.auth,1) }">
							√
						</c:if>
					</td>
					<td>
						<c:if test="${authUtil:isAuth(admin.auth,2) }">
							√
						</c:if>
					</td>
					<td>
						<c:if test="${authUtil:isAuth(admin.auth,3) }">
							√
						</c:if>
					</td>
					<td>
						<c:if test="${authUtil:isAuth(admin.auth,4) }">
							√
						</c:if>
					</td>
					<td>
						<c:if test="${authUtil:isAuth(admin.auth,5) }">
							√
						</c:if>
					</td>
					<td>
						<c:if test="${authUtil:isAuth(admin.auth,6) }">
							√
						</c:if>
					</td>
					<td>${admin.regDate }</td>
					<td>
						<button class="changeButton" onclick="changeButtonFun(${admin.id})">修改</button>
						<button onclick="deleteButtonFun(${admin.id})">删除</button>
					</td>
				</tr>
			</c:forEach>
		</table>
	</c:if>
	<div id="add">
		<input type="hidden" id="adminId">
		<label>用户名:<input type="text" id="username"></label><br>
		<label>密&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" id="password"></label><br>
		<label>重复密码：<input type="password" id="rePassword"></label><br>
		<label>姓&nbsp;&nbsp;&nbsp;&nbsp;名：<input type="text" id="name"></label><br>
		身份选择：
		<select id="roleCode" onchange="setAuth()">
			<option value=0>人力管理</option>
			<option value=1>房间管理</option>
			<option value=2>前台</option>
			<option value=3>财务管理</option>
		</select><br>
		附加权限：<br>
		<input id="auth" type="checkbox" value="0" disabled checked>用户数据管理<br>
		<input id="auth" type="checkbox" value="1">房间类型管理<br>
		<input id="auth" type="checkbox" value="2">房间数据管理<br>
		<input id="auth" type="checkbox" value="3">入住信息管理<br>
		<input id="auth" type="checkbox" value="4">换房信息管理<br>
		<input id="auth" type="checkbox" value="5">续住信息管理<br>
		<input id="auth" type="checkbox" value="6">财务信息管理<br>
		<textarea cols="20" rows="20" id="zcomment"></textarea><br>
		<button id="change">修改</button>
		<button id="submit">提交</button>
		<button id="cancel">取消</button>
	</div>
	
	<style>
		#add{
			display:none;
		}
		#change{
			display:none;
		}
	</style>
	<script type="text/javascript">
		function clearContent(){
			$('#username').val('')
			document.getElementById('password').disabled=false;
			document.getElementById('rePassword').disabled=false;
			$('#name').val('')
			$('#roleCode').val(0)
			setRoleAuth(0)
			$('#zcomment').text('');
		}
		function setRoleAuth(code){
			v1=new Array(1,0,0,0,0,0,0);v2=new Array(0,1,1,0,0,0,0);v3=new Array(0,0,0,1,1,1,0);v4=new Array(0,0,0,0,0,0,1)
			arrays=new Array(v1,v2,v3,v4)
			//alert(arrays[1][0])
			auths=$("input[type='checkbox']")
			for(var i=0;i<auths.length;i++){
				//alert(i)
				if(arrays[code][i]==1){
					auths[i].disabled=true
					auths[i].checked=true
				}else{
					auths[i].disabled=false
					auths[i].checked=false
				}
			}
		}
		function setAuth(){
			//alert("test")
			code=$("#roleCode").val()
			setRoleAuth(code)
			//alert(auth[0].value)
		}
		function changeButtonFun(id){
			var add=document.getElementById('add')
			add.style.display='block'
			var change=document.getElementById('change')
			change.style.display='inline'
			var submit=document.getElementById('submit')
			submit.style.display='none'
			$.get("/hotel/admin/getAdminInfor?id="+id,{},function(res){
				var ad=eval('(' +res + ')').object
				//alert(ad.username)
				$('#adminId').val(ad.id)
				$('#username').val(ad.username)
				document.getElementById('password').disabled=true;
				document.getElementById('rePassword').disabled=true;
				$('#name').val(ad.name)
				$('#roleCode').val(ad.roleCode)
				auths=$("input[type='checkbox']")
				authNum=ad.auth
				//alert(authNum)
				setRoleAuth(ad.roleCode)
				for(var i=0;i<auths.length;i++){
					auths[i].checked=((authNum>>i&1)==1)
					//alert(authNum)
					//authNum/=2
				}
				//
				$('#zcomment').text(ad.zcomment);
				
				//$('#password').disabled=true;
			})
		}
		$("#addButton").click(function(){
			clearContent()
			var add=document.getElementById('add')
			add.style.display='block'
			var change=document.getElementById('change')
			change.style.display='none'
			var submit=document.getElementById('submit')
			submit.style.display='inline'
		})
		$("#cancel").click(function(){
			var add=document.getElementById('add')
			add.style.display='none'
			clearContent()
		})
		$("#submit").click(function(){
			checks=$("input[type='checkbox']:checked")
			auth=0
			for(var i=0;i<checks.length;i++){
				auth|=1<<checks[i].value
			}
			p1=$("#password").val();p2=$("#rePassword").val()
			if(p1!=p2){
				alert("密码两次输入不一致！")
				return ;
			}
			data={"username":$("#username").val(),"password":p1,"name":$("#name").val(),
					"roleCode":$("#roleCode").val(),"auth":auth,"zcomment":$("#zcomment").val()
			}
			//alert(JSON.stringify(data))
			$.post("/hotel/admin/addNewAdmin.do",data,function(str){
				var res=eval('(' +str + ')')
				//alert(res.message);
				if(res.code==0){
					window.location.href="/hotel/system/mainPage"
				}else{
					alert(res.message);
				}
			});
			return ;
		})
		$("#change").click(function(){
			checks=$("input[type='checkbox']:checked")
			auth=0
			for(var i=0;i<checks.length;i++){
				auth|=1<<checks[i].value
			}
			p1=$("#password").val();p2=$("#rePassword").val()
			if(p1!=p2){
				alert("密码两次输入不一致！")
				return ;
			}
			data={"id":$("#adminId").val(),"username":$("#username").val(),"name":$("#name").val(),
					"roleCode":$("#roleCode").val(),"auth":auth,"zcomment":$("#zcomment").val()
			}
			//alert(JSON.stringify(data))
			$.post("/hotel/admin//changeAdminInfor.do",data,function(str){
				var res=eval('(' +str + ')')
				//alert(res.message);
				if(res.code==0){
					window.location.href="/hotel/system/mainPage"
				}else{
					alert(res.message);
				}
			});
			return ;
		})
		function deleteButtonFun(id){
			if(confirm("您确定要删除该用户吗？")){
				$.post("/hotel/admin/deleteAdmin.do",{"id":id},function(str){
					var res=eval('(' +str + ')')
					//alert(res.message);
					if(res.code==0){
						window.location.href="/hotel/system/mainPage"
					}else{
						alert(res.message);
					}
				});
			}
		}
	</script>
</html>